package com.tronsis.distribution.activity.me.agent;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.tronsis.distribution.R;
import com.tronsis.distribution.bean.MyStockBean;
import com.tronsis.distribution.bean.ProductDetailBean;
import com.tronsis.distribution.biz.factory.OrderBizFactory;
import com.tronsis.distribution.biz.factory.UpgradeBizFactory;
import com.tronsis.distribution.http.Url;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.adapter.recyclerview.CommonAdapter;
import com.tronsis.library.adapter.recyclerview.base.ViewHolder;
import com.tronsis.library.adapter.recyclerview.wrapper.LoadMoreWrapper;
import com.tronsis.library.http.ListBean;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.util.DisplayUtil;
import com.tronsis.library.widget.MyDecoration;
import com.tronsis.library.widget.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AgentExitGoodsActivity extends ToolBarBaseActivity {

    @BindView(R.id.rv_goods)
    RecyclerView mRvGoods;

    private int mPage = 1;
    private int mSize = 15;
    private int mTotal;

    private CommonAdapter<ProductDetailBean> mAdapter;
    private List<ProductDetailBean> mGoodsList = new ArrayList<>();

    private ArrayList<MyStockBean> mMyStockList = new ArrayList<>();
    private LoadMoreWrapper mLoadMoreWrapper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agent_exit_goods);
        ButterKnife.bind(this);
        setTitle(true, "产品信息");

        String quitId = getIntent().getStringExtra("quitId");
        if (quitId != null) {
            initRv();
            httpGetGoodsList(quitId);
        } else {
            initMyRv();
            httpGetStockList(true, mPage, mSize);
        }
    }

    private void initRv() {
        mAdapter = new CommonAdapter<ProductDetailBean>(mContext, mGoodsList, R.layout.item_order_goods) {
            @Override
            protected void convert(final ViewHolder holder, final ProductDetailBean bean, final int position) {
                holder.setImageByUrl(mContext, R.id.iv_cover, Url.FILE_DOWNLOAD + bean.getCoverFid());
                holder.setText(R.id.tv_title, bean.getName());
                holder.setText(R.id.tv_count, "x" + bean.getAmount());
            }
        };

        int height = DisplayUtil.getInstance(mContext).dp2px(1);
        int color = Color.parseColor("#F4F4F4");
        RecycleViewDivider decoration = new RecycleViewDivider(mContext, MyDecoration.VERTICAL_LIST, height, color);
        mRvGoods.addItemDecoration(decoration);
        mRvGoods.setLayoutManager(new LinearLayoutManager(mContext));
        mRvGoods.setHasFixedSize(true);
        mRvGoods.setNestedScrollingEnabled(false);
        mRvGoods.setAdapter(mAdapter);
    }

    private void initMyRv() {
        CommonAdapter<MyStockBean> adapter = new CommonAdapter<MyStockBean>(mContext, mMyStockList, R.layout.item_stock) {
            @Override
            protected void convert(ViewHolder holder, MyStockBean stockBean, int position) {
                holder.setImageByUrl(mContext, R.id.iv_cover, Url.FILE_DOWNLOAD + stockBean.getCoverFid());
                holder.setText(R.id.tv_product_name, stockBean.getName());
                holder.setText(R.id.tv_amount, "x" + stockBean.getAmount());
            }
        };

        mLoadMoreWrapper = new LoadMoreWrapper(adapter);
        mLoadMoreWrapper.setLoadMoreView(R.layout.rv_load_more);
        mLoadMoreWrapper.setOnLoadMoreListener(new LoadMoreWrapper.OnLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (mMyStockList.size() % 15 == 0 && mMyStockList.size() > 0 && mMyStockList.size() < mTotal) {
                    mPage = (mMyStockList.size() / 15) + 1;
                    httpGetStockList(false, mPage, mSize);
                }
            }
        });
        int height = DisplayUtil.getInstance(mContext).dp2px(1);
        int color = Color.parseColor("#F4F4F4");
        RecycleViewDivider decoration = new RecycleViewDivider(mContext, MyDecoration.VERTICAL_LIST, height, color);
        mRvGoods.addItemDecoration(decoration);
        mRvGoods.setLayoutManager(new LinearLayoutManager(mContext));
        mRvGoods.setHasFixedSize(true);
        mRvGoods.setNestedScrollingEnabled(false);
        mRvGoods.setAdapter(mLoadMoreWrapper);
    }

    private void httpGetGoodsList(String quitId) {
        if (quitId == null) {
            return;
        }
        try {
            UpgradeBizFactory.createBiz().quitProductInfo(this, quitId, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    List<ProductDetailBean> list = (List<ProductDetailBean>) data;

                    mGoodsList.clear();
                    for (ProductDetailBean productDetailBean : list) {
                        if (productDetailBean.getAmount() > 0) {
                            mGoodsList.add(productDetailBean);
                        }
                    }

                    mAdapter.notifyDataSetChanged();
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();
                }
            });
        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }
    }

    private void httpGetStockList(final boolean isRefresh, int page, int size) {
        try {
            OrderBizFactory.createBiz().getMyStockList(this, page, size, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    ListBean<MyStockBean> listBean = (ListBean<MyStockBean>) data;
                    mTotal = listBean.getTotal();
                    List<MyStockBean> list = listBean.getList();

                    if (isRefresh) {
                        mMyStockList.clear();
                    }

                    for (MyStockBean myStockBean : list) {
                        if (myStockBean.getAmount() > 0) {
                            mMyStockList.add(myStockBean);
                        }
                    }

                    mLoadMoreWrapper.notifyDataSetChanged();
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            dismissLoadingDialog();
        }
    }
}
